package com.tlm.consumer;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@RestController
public class ConsumerController {
    @GetMapping("testGet")
//    @GetMapping("testGet111")
    public void testGet(){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet("http://localhost:7010/testProvider");
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("testRest")
    public String testRest(String name){
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://localhost:7010/testProvider?name="+name, String.class);
        System.out.println("数据"+forEntity.getBody());
        System.out.println("状态码"+forEntity.getStatusCode());
        return forEntity.getBody();
    }
    @GetMapping("testRestKua")
    public String testRestKua(String name){
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://provider/testProvider?name="+name, String.class);
        System.out.println("数据"+forEntity.getBody());
        System.out.println("状态码"+forEntity.getStatusCode());
        return forEntity.getBody();
    }
    @GetMapping("testRestPost1")
    public UserVo testRestPost1(String name){
        UserVo userVo = new UserVo();
        userVo.setName(name);
        userVo.setId(1);
        userVo.setAge("12");
       int num = (int)(Math.random()*3);
       switch (num){
           case 1: UserVo userVo1 = restTemplate.postForObject("http://localhost:7010/testProviderPost", userVo, UserVo.class);
                   System.out.println("数据"+userVo1);
                   return userVo1;
           case 2: UserVo userVo2 = restTemplate.postForObject("http://localhost:7020/testProviderPost", userVo, UserVo.class);
                   System.out.println("数据"+userVo2);
                   return userVo2;
       }
        return null;
    }
    @GetMapping("testRestPost")
    public UserVo testRestPost(String name){
        UserVo userVo = new UserVo();
        userVo.setName(name);
        userVo.setId(1);
        userVo.setAge("12");
        int a = 1;
        if(a==1){
            UserVo userVo1 = restTemplate.postForObject("http://localhost:7010/testProviderPost", userVo, UserVo.class);
            System.out.println("数据"+userVo1);
            a=2;
            return userVo1;
        }if(a==2){
            UserVo userVo1 = restTemplate.postForObject("http://localhost:7020/testProviderPost", userVo, UserVo.class);
            System.out.println("数据"+userVo1);
            a=1;
            return userVo1;
        }
        return null;
    }

    @GetMapping("testRestPost3")
    public UserVo testRestPost3(String name){
        UserVo userVo = new UserVo();
        userVo.setName(name);
        userVo.setId(1);
        userVo.setAge("12");
        UserVo userVo1 = restTemplate.postForObject("http://localhost:7010/testProviderPost", userVo, UserVo.class);
        System.out.println("数据"+userVo1);


        return null;
    }
}
